﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using Xwang.Task.Model;

namespace Xwang.Task.Dal
{
    /// <summary>
    ///  数据库上下文工厂，保证线程内上下文唯一
    /// </summary>
    public class ContextFactory
    {
        /// <summary>
        /// 获取当前上下文，线程内唯一
        /// </summary>
        /// <returns></returns>
        public static DbContext GetCurrentDbContext()
        {
            var db = (DbContext)CallContext.GetData("DbContext");
            if (db != null) return db;
            db = new TaskModelContainer();
            CallContext.SetData("DbContext", db);
            return db;
        }
    }
}
